package com.lovo.project.service.impl;

import com.lovo.project.dao.ServiceDAO;
import com.lovo.project.enums.ServiceCodeEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.model.ServiceDO;
import com.lovo.project.service.ServiceService;
import com.lovo.project.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author YI只小阿帅丶
 * @title 服务业务层实现类
 * @date 2020/11/4 11:37
 */

public class ServiceServiceImpl implements ServiceService {
    @Override
    public List<ServiceDO> listAll() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        ServiceDAO serviceDAO = sqlSession.getMapper(ServiceDAO.class);
        try {
            List<ServiceDO> list = serviceDAO.listAll();
            for (ServiceDO obj : list) {
                String time = obj.getServiceSchedule();
                 
            }
            if (list.size() <= 0) {
                throw new OurException(ServiceCodeEnum.SERVICE_FIND_ERROR);
            }
            return list;
        } catch (OurException e) {
            throw e;
        } finally {
            sqlSession.close();
        }
    }

    @Override
    public Integer insertService(ServiceDO serviceDO) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        ServiceDAO serviceDAO = sqlSession.getMapper(ServiceDAO.class);
        try {
            Integer integer = serviceDAO.insertService(serviceDO);
            if (integer == 0) {
                throw new OurException(ServiceCodeEnum.SERVICE_ADD_ERROR);
            }
            sqlSession.commit();
            return integer;
        } catch (OurException e) {
            sqlSession.rollback();
            throw e;
        } finally {
            sqlSession.close();
        }
    }

    @Override
    public Integer deleteService(Integer serviceId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        ServiceDAO serviceDAO = sqlSession.getMapper(ServiceDAO.class);
        try {
            Integer integer = serviceDAO.deleteService(serviceId);
            if (integer == 0) {
                throw new OurException(ServiceCodeEnum.SERVICE_DELETE_ERROR);
            }
            sqlSession.commit();
            return integer;
        } catch (OurException e) {
            sqlSession.rollback();
            throw e;
        } finally {
            sqlSession.close();
        }
    }

    @Override
    public Integer updateService(ServiceDO serviceDO) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        ServiceDAO serviceDAO = sqlSession.getMapper(ServiceDAO.class);
        try {
            Integer integer = serviceDAO.updateService(serviceDO);
            if (integer == 0) {
                throw new OurException(ServiceCodeEnum.SERVICE_UPDATE_ERROR);
            }
            sqlSession.commit();
            return integer;
        } catch (OurException e) {
            sqlSession.rollback();
            throw e;
        } finally {
            sqlSession.close();
        }
    }

    @Override
    public ServiceDO selectByServiceId(Integer serviceId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        ServiceDAO serviceDAO = sqlSession.getMapper(ServiceDAO.class);
        try {
            ServiceDO serviceDO = serviceDAO.selectByServiceId(serviceId);
            if (serviceDO == null) {
                throw new OurException(ServiceCodeEnum.SERVICE_FIND_ERROR);
            }
            sqlSession.commit();
            return serviceDO;
        } catch (OurException e) {
            throw e;
        } finally {
            sqlSession.close();
        }
    }

    @Override
    public List<ServiceDO> listByShopId(Integer shopId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        ServiceDAO serviceDAO = sqlSession.getMapper(ServiceDAO.class);
        try {
            List<ServiceDO> list = serviceDAO.listByShopId(shopId);
            if (list.size() == 0) {
                throw new OurException(ServiceCodeEnum.SERVICE_FIND_ERROR);
            }
            sqlSession.commit();
            return list;
        } catch (OurException e) {
            throw e;
        } finally {
            sqlSession.close();
        }
    }
}
